1 module hip.jni.android.native_window; 2 import hip.jni.android.android_api; 3 4 /* 5 * Copyright (C) 2010 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 /** 21 * @defgroup ANativeWindow Native Window 22 * 23 * ANativeWindow represents the producer end of an image queue. 24 * It is the C counterpart of the android.view.Surface object in Java, 25 * and can be converted both ways. Depending on the consumer, images 26 * submitted to ANativeWindow can be shown on the display or sent to 27 * other consumers, such as video encoders. 28 * @{ 29 */ 30 31 /** 32 * @file native_window.h 33 * @brief API for accessing a native window. 34 */ 35 36 import core.stdc.stdint; 37 // #include <sys/cdefs.h> 38 39 import hip.jni.android.data_space; 40 import hip.jni.android.hardware_buffer; 41 import hip.jni.android.rect; 42 43 44 extern(C): 45 /** 46 * Legacy window pixel format names, kept for backwards compatibility. 47 * New code and APIs should use AHARDWAREBUFFER_FORMAT_*. 48 */ 49 enum ANativeWindow_LegacyFormat { 50 // NOTE: these values must match the values from graphics/common/x.x/types.hal 51 52 /** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Alpha: 8 bits. **/ 53 WINDOW_FORMAT_RGBA_8888 = AHardwareBuffer_Format.AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, 54 /** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Unused: 8 bits. **/ 55 WINDOW_FORMAT_RGBX_8888 = AHardwareBuffer_Format.AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM, 56 /** Red: 5 bits, Green: 6 bits, Blue: 5 bits. **/ 57 WINDOW_FORMAT_RGB_565 = AHardwareBuffer_Format.AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM, 58 } 59 60 /** 61 * Transforms that can be applied to buffers as they are displayed to a window. 62 * 63 * Supported transforms are any combination of horizontal mirror, vertical 64 * mirror, and clockwise 90 degree rotation, in that order. Rotations of 180 65 * and 270 degrees are made up of those basic transforms. 66 */ 67 enum ANativeWindowTransform { 68 ANATIVEWINDOW_TRANSFORM_IDENTITY = 0x00, 69 ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL = 0x01, 70 ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL = 0x02, 71 ANATIVEWINDOW_TRANSFORM_ROTATE_90 = 0x04, 72 73 ANATIVEWINDOW_TRANSFORM_ROTATE_180 = ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL | 74 ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL, 75 ANATIVEWINDOW_TRANSFORM_ROTATE_270 = ANATIVEWINDOW_TRANSFORM_ROTATE_180 | 76 ANATIVEWINDOW_TRANSFORM_ROTATE_90, 77 } 78 79 /** 80 * Opaque type that provides access to a native window. 81 * 82 * A pointer can be obtained using {@link ANativeWindow_fromSurface()}. 83 */ 84 struct ANativeWindow; 85 86 /** 87 * Struct that represents a windows buffer. 88 * 89 * A pointer can be obtained using {@link ANativeWindow_lock()}. 90 */ 91 struct ANativeWindow_Buffer { 92 /// The number of pixels that are shown horizontally. 93 int32_t width; 94 95 /// The number of pixels that are shown vertically. 96 int32_t height; 97 98 /// The number of *pixels* that a line in the buffer takes in 99 /// memory. This may be >= width. 100 int32_t stride; 101 102 /// The format of the buffer. One of AHardwareBuffer_Format. 103 int32_t format; 104 105 /// The actual bits. 106 void* bits; 107 108 /// Do not touch. 109 uint32_t[6] reserved; 110 } 111 112 /** 113 * Acquire a reference on the given {@link ANativeWindow} object. This prevents the object 114 * from being deleted until the reference is removed. 115 */ 116 void ANativeWindow_acquire(ANativeWindow* window); 117 118 /** 119 * Remove a reference that was previously acquired with {@link ANativeWindow_acquire()}. 120 */ 121 void ANativeWindow_release(ANativeWindow* window); 122 123 /** 124 * Return the current width in pixels of the window surface. 125 * 126 * \return negative value on error. 127 */ 128 int32_t ANativeWindow_getWidth(ANativeWindow* window); 129 130 /** 131 * Return the current height in pixels of the window surface. 132 * 133 * \return a negative value on error. 134 */ 135 int32_t ANativeWindow_getHeight(ANativeWindow* window); 136 137 /** 138 * Return the current pixel format (AHARDWAREBUFFER_FORMAT_*) of the window surface. 139 * 140 * \return a negative value on error. 141 */ 142 int32_t ANativeWindow_getFormat(ANativeWindow* window); 143 144 /** 145 * Change the format and size of the window buffers. 146 * 147 * The width and height control the number of pixels in the buffers, not the 148 * dimensions of the window on screen. If these are different than the 149 * window's physical size, then its buffer will be scaled to match that size 150 * when compositing it to the screen. The width and height must be either both zero 151 * or both non-zero. 152 * 153 * For all of these parameters, if 0 is supplied then the window's base 154 * value will come back in force. 155 * 156 * \param width width of the buffers in pixels. 157 * \param height height of the buffers in pixels. 158 * \param format one of the AHardwareBuffer_Format constants. 159 * \return 0 for success, or a negative value on error. 160 */ 161 int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window, 162 int32_t width, int32_t height, int32_t format); 163 164 /** 165 * Lock the window's next drawing surface for writing. 166 * inOutDirtyBounds is used as an in/out parameter, upon entering the 167 * function, it contains the dirty region, that is, the region the caller 168 * intends to redraw. When the function returns, inOutDirtyBounds is updated 169 * with the actual area the caller needs to redraw -- this region is often 170 * extended by {@link ANativeWindow_lock}. 171 * 172 * \return 0 for success, or a negative value on error. 173 */ 174 int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer, 175 ARect* inOutDirtyBounds); 176 177 /** 178 * Unlock the window's drawing surface after previously locking it, 179 * posting the new buffer to the display. 180 * 181 * \return 0 for success, or a negative value on error. 182 */ 183 int32_t ANativeWindow_unlockAndPost(ANativeWindow* window); 184 185 static if(__ANDROID_API__ >= 26) 186 { 187 /** 188 * Set a transform that will be applied to future buffers posted to the window. 189 * 190 * Available since API level 26. 191 * 192 * \param transform combination of {@link ANativeWindowTransform} flags 193 * \return 0 for success, or -EINVAL if \p transform is invalid 194 */ 195 int32_t ANativeWindow_setBuffersTransform(ANativeWindow* window, int32_t transform); 196 } 197 198 static if(__ANDROID_API__ >= 28) 199 { 200 /** 201 * All buffers queued after this call will be associated with the dataSpace 202 * parameter specified. 203 * 204 * dataSpace specifies additional information about the buffer. 205 * For example, it can be used to convey the color space of the image data in 206 * the buffer, or it can be used to indicate that the buffers contain depth 207 * measurement data instead of color images. The default dataSpace is 0, 208 * ADATASPACE_UNKNOWN, unless it has been overridden by the producer. 209 * 210 * Available since API level 28. 211 * 212 * \param dataSpace data space of all buffers queued after this call. 213 * \return 0 for success, -EINVAL if window is invalid or the dataspace is not 214 * supported. 215 */ 216 int32_t ANativeWindow_setBuffersDataSpace(ANativeWindow* window, int32_t dataSpace); 217 218 /** 219 * Get the dataspace of the buffers in window. 220 * 221 * Available since API level 28. 222 * 223 * \return the dataspace of buffers in window, ADATASPACE_UNKNOWN is returned if 224 * dataspace is unknown, or -EINVAL if window is invalid. 225 */ 226 int32_t ANativeWindow_getBuffersDataSpace(ANativeWindow* window); 227 228 } 229 230 static if(__ANDROID_API__ >= 30) 231 { 232 /** Compatibility value for ANativeWindow_setFrameRate. */ 233 enum ANativeWindow_FrameRateCompatibility { 234 /** 235 * There are no inherent restrictions on the frame rate of this window. 236 */ 237 ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT = 0, 238 /** 239 * This window is being used to display content with an inherently fixed 240 * frame rate, e.g. a video that has a specific frame rate. When the system 241 * selects a frame rate other than what the app requested, the app will need 242 * to do pull down or use some other technique to adapt to the system's 243 * frame rate. The user experience is likely to be worse (e.g. more frame 244 * stuttering) than it would be if the system had chosen the app's requested 245 * frame rate. 246 */ 247 ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE = 1 248 } 249 250 /** 251 * Sets the intended frame rate for this window. 252 * 253 * On devices that are capable of running the display at different refresh 254 * rates, the system may choose a display refresh rate to better match this 255 * window's frame rate. Usage of this API won't introduce frame rate throttling, 256 * or affect other aspects of the application's frame production 257 * pipeline. However, because the system may change the display refresh rate, 258 * calls to this function may result in changes to Choreographer callback 259 * timings, and changes to the time interval at which the system releases 260 * buffers back to the application. 261 * 262 * Note that this only has an effect for windows presented on the display. If 263 * this ANativeWindow is consumed by something other than the system compositor, 264 * e.g. a media codec, this call has no effect. 265 * 266 * Available since API level 30. 267 * 268 * \param frameRate The intended frame rate of this window, in frames per 269 * second. 0 is a special value that indicates the app will accept the system's 270 * choice for the display frame rate, which is the default behavior if this 271 * function isn't called. The frameRate param does <em>not</em> need to be a 272 * valid refresh rate for this device's display - e.g., it's fine to pass 30fps 273 * to a device that can only run the display at 60fps. 274 * 275 * \param compatibility The frame rate compatibility of this window. The 276 * compatibility value may influence the system's choice of display refresh 277 * rate. See the ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_* values for more info. 278 * 279 * \return 0 for success, -EINVAL if the window, frame rate, or compatibility 280 * value are invalid. 281 */ 282 int32_t ANativeWindow_setFrameRate(ANativeWindow* window, float frameRate, int8_t compatibility); 283 /** 284 * Provides a hint to the window that buffers should be preallocated ahead of 285 * time. Note that the window implementation is not guaranteed to preallocate 286 * any buffers, for instance if an implementation disallows allocation of new 287 * buffers, or if there is insufficient memory in the system to preallocate 288 * additional buffers 289 * 290 * Available since API level 30. 291 */ 292 void ANativeWindow_tryAllocateBuffers(ANativeWindow* window); 293 }